
package com.yimixian.app.goods;

import com.yimixian.app.R;
import com.yimixian.app.activity.BaseNaviFragmentGroupActivity;
import com.yimixian.app.activity.YMXMainActivity;
import com.yimixian.app.ui.FragmentGroup;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.UiUtils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import java.util.List;

import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;
import de.greenrobot.event.EventBus;

public class GoodDetailActivity extends BaseNaviFragmentGroupActivity implements
        View.OnClickListener {
    public static final String INTENT_EXTRA_GOOD_ID = "good_id";
    private String mGoodId = "";

    public FrameLayout mDialogContainer;
    private FrameLayout mGuideLayout;
    public ImageView mCommentGuildView;
    public ShareGoodView mShareLayout;
    public FrameLayout mMasking;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navibar_simple);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        mGuideLayout = (FrameLayout) findViewById(R.id.fl_guide);
        mCommentGuildView = (ImageView) findViewById(R.id.iv_guide_comment);
        findViewById(R.id.title_bar).setVisibility(View.GONE);
        mShareLayout = (ShareGoodView) findViewById(R.id.share_layout);
        mMasking = (FrameLayout) findViewById(R.id.fl_mask);
        mDialogContainer = (FrameLayout) findViewById(R.id.fl_dialog_container);
        mGoodId = getIntent().getExtras().getString(INTENT_EXTRA_GOOD_ID);
        mGuideLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (SharedPreferencesHelper.getGuideComment()) {
                    return;
                }
                if (mGuideLayout != null && mCommentGuildView != null) {
                    mGuideLayout.setVisibility(View.GONE);
                    mCommentGuildView.setVisibility(View.GONE);
                    SharedPreferencesHelper.setGuideComment(true);
                }
            }
        });
        mMasking.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mShareLayout != null) {
                    cancelMasking();
                    mShareLayout.closeShareViewWithAnim();
                }
            }
        });
        showGuideView();
    }

    private void cancelMasking() {
        AlphaAnimation animation = new AlphaAnimation(1.0f,0.0f);
        animation.setDuration(200);
        if(mMasking != null) {
            mMasking.startAnimation(animation);
            mMasking.setVisibility(View.GONE);
        }
    }

    public void startMasking() {
        if(mMasking != null) {
            mMasking.setVisibility(View.VISIBLE);
        }
        AlphaAnimation animation = new AlphaAnimation(0.0f,1.0f);
        animation.setDuration(200);
        if(mMasking != null) {
            mMasking.startAnimation(animation);
        }
    }

    /**
     * 显示引导页
     */
    private void showGuideView() {
        if (!SharedPreferencesHelper.getGuideComment()) {
            mGuideLayout.setVisibility(View.VISIBLE);
            if (mCommentGuildView.getVisibility() != View.VISIBLE) {
                //这里的显示在GoodDetailTopFragment中控制
//                mCommentGuildView.setVisibility(View.VISIBLE);
            }
        }
    }

    public static Intent buildIntent(Context context, String goodId) {
        Intent intent = new Intent(context, GoodDetailActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        intent.putExtra(INTENT_EXTRA_GOOD_ID, goodId);
        return intent;
    }

    @Override
    protected void initPrimaryFragment() {
        switchPrimaryFragment(0);
    }

    @Override
    protected Class<? extends Fragment> getPrimaryFragmentClass(int fragmentId) {
        return GoodDetailFragmentGroup.class;
    }

    @Override
    protected Bundle getPrimaryFragmentArguments(int fragmentId) {
        Bundle bundle = new Bundle();
        bundle.putString(INTENT_EXTRA_GOOD_ID, mGoodId);
        return bundle;
    }

    @Override
    protected int getPrimaryFragmentStubId(int fragmentId) {
        return R.id.content_frame;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mShareLayout != null) {
            if (mMasking != null) {
                mMasking.setVisibility(mShareLayout.getVisibility());
            }
        } else {
            if (mMasking != null) {
                mMasking.setVisibility(View.GONE);
            }
        }
        if(YMXMainActivity.needChooseKindTab){
            setResult(GoodDetailTopFragment.RETURN_TO_CATEGORY);
            finish();
        }
    }

    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_bar_right_text:

                break;

            case R.id.title_bar_left_btn:
                // onBackPressed();
                break;

            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if(UiUtils.isHasDialog(this)){
            return;
        }
        if (mCurrentPrimaryFragment != null
                && mCurrentPrimaryFragment instanceof GoodDetailTopFragment) {
            // ShareGoodView shareView = ((GoodDetailTopFragment)
            // mCurrentPrimaryFragment)
            // .getShareGoodView();
            if (mShareLayout != null && mShareLayout.getVisibility() == View.VISIBLE) {
                if (mMasking != null) {
                    cancelMasking();
                }
                mShareLayout.closeShareViewWithAnim();
            } else {
                super.onBackPressed();
            }
        } else {
            super.onBackPressed();
        }
    }

    public void finishWithResult(int resultCode){
        YMXMainActivity.needChooseKindTab = true;
        setResult(resultCode);
        finish();
    }

    public Fragment getPrimaryFragmentGroup() {
        return mCurrentPrimaryFragment;
    }

    public void onEventMainThread(String message) {
        if (message != null) {
            if (ShareGoodView.SHAREVIEW_CANCEL_MASKING.equals(message)) {
                if (mMasking != null) {
                    cancelMasking();
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        for(Fragment fragment : fragments){
            if(fragment instanceof GoodDetailFragmentGroup){
                GoodDetailFragmentGroup group = ((GoodDetailFragmentGroup) fragment);
                List<Fragment> childs = group.getChildFragmentManager().getFragments();
                for (Fragment child : childs){
                    if(child instanceof GoodDetailTopFragment){
                        AutoScrollViewPager bannersView = ((GoodDetailTopFragment) child).mBannersView.getBannersView();
                        if(bannersView != null)
                            bannersView.startAutoScroll();
                        return;
                    }
                }
            }
        }
    }
}
